package com.laulkar.activities;

import java.util.List;

import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import com.laulkar.R;
import com.laulkar.dataproviders.Alarm;
import com.laulkar.dataproviders.AlarmDbAdapter;


public class AlarmLister extends ListActivity {

	private AlarmAdapter m_adapter;
	private List<Alarm> subway_status;

	public static final String SUBWAY_STATUS_KEY = "key";
	private static final int ADDHOST = 0;
	
	private static final int HELP = 1;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.alarm_list_main);
		//Bundle extras = getIntent().getExtras();
		//subway_status = extras.getParcelableArrayList(SUBWAY_STATUS_KEY);
		//setDisplay();
		//this.subway_status = Main.mta_status.subways;
		AlarmDbAdapter mDbHelper = new AlarmDbAdapter(this);
		mDbHelper.open();
		subway_status = mDbHelper.fetchAllAlarmsList();
		mDbHelper.close();
		this.m_adapter = new AlarmAdapter(this, R.layout.alarm_list_main, subway_status);
		setListAdapter(this.m_adapter);

		setButtonListners();
	}


	//	protected void onListItemClick(ListView l, View v, int position, long id) 
	//	{
	//		super.onListItemClick(l, v, position, id);
	//		Subway s;
	//
	//		s = (Subway) getListView().getItemAtPosition(position);
	//
	//		showSubwayStatus(s);
	//
	//	}


	private void setButtonListners() {
		/*
		Button b = (Button) findViewById(R.id.alarm_list_add);
		b.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				Intent i = new Intent(AlarmLister.this, TimeSelector.class);
				startActivity(i);
			}
		});
*/

	}
	public void onDestroy()
	{
		super.onDestroy();
	}
	
	
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);

		MenuItem item;

		item = menu.add(0, ADDHOST,0, R.string.add_alarm);
		item.setIcon(android.R.drawable.ic_menu_add);
		
		item = menu.add(0, HELP,0, R.string.help);
		item.setIcon(android.R.drawable.ic_menu_help);

	

		return true;
	}

	public boolean onMenuItemSelected(int featureId, MenuItem item) {
		switch(item.getItemId()) {

		case ADDHOST:
			createAlarm();
			break;

		case HELP:
			displayHelp();
			break;
		}

		return super.onMenuItemSelected(featureId, item);
	}


	private void displayHelp() {
		new AlertDialog.Builder(this).setMessage(R.string.about_text).
		setPositiveButton(android.R.string.ok, null).show();
		
	}


	private void createAlarm() {
		Intent i = new Intent(AlarmLister.this, TimeSelector.class);
		startActivity(i);
	}


	private class AlarmAdapter extends ArrayAdapter<Alarm> {

		private List<Alarm> items;

		public AlarmAdapter(Context context, int textViewResourceId, List<Alarm> items) {
			super(context, textViewResourceId, items);
			this.items = items;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			View v = convertView;
			if (v == null) {
				LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				v = vi.inflate(R.layout.alarm_list_row, null);
			}
			Alarm a = items.get(position);
			if (a != null) {
				TextView lines = (TextView) v.findViewById(R.id.alarm_list_lines);
				TextView start = (TextView) v.findViewById(R.id.alarm_list_row_start);
				TextView end = (TextView) v.findViewById(R.id.alarm_list_row_end);
				lines.setText(a.line);
				String s = (a.start / 100) + ":" + (a.start % 100); 


				start.setText(s);
				s = (a.end / 100) + ":" + (a.end % 100);
				end.setText(s);
				/*
				if (status != null) {
					status.setText(s.getStatus()); 
					if(s.getStatus().equalsIgnoreCase(Constants.GOOD_SERVICE))
					{
						status.setTextColor(Color.GREEN);
					}
				}
				 */
			}
			return v;
		}
	}
}
